package com.UBC417.termproject.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.UBC417.termproject.Data.*;
import com.google.appengine.api.datastore.EntityNotFoundException;

@SuppressWarnings("serial")
public class ShowHistoryServlet extends HttpServlet{
	private static final Logger log = Logger.getLogger(SubscribeServlet.class.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		// Get parameters		
		HttpSession session = req.getSession();
		String userId = (String) session.getAttribute("id");

		
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out= resp.getWriter();
		
		List<Bid> entries=Buyer.ShowActiveBids(userId);
		log.info("history user id: "+ userId);
		if (entries.size()==0){
			//no results
			out.println("NO BIDDING HISTORY!");
		}else{
			resp.getWriter().println("<table border='1'>");
			out.println("<tr>");
			//out.println("<td>Auction ID</td>");
			out.println("<td>Item Name</td>");
			out.println("<td>Seller</td>");
			out.println("<td>Bid Price</td>");
			out.println("<td>Bid Date</td>");
			out.println("<td>Status</td>");	
			out.println("<td>Highest Bid</td>");
			out.println("<td>Bid</td>");
			out.println("</tr>");
			for (Bid e : entries){
				try {
					Auction a = User.getAuctionInfo(e.getAuctionId());
					Bid b= User.getHighestBid(e.getAuctionId());
					Item i= User.getAuctionItem(e.getAuctionId());
					out.println("<tr>");
					//out.println("<td>"+e.getAuctionId()+"</td>");
					out.println("<td>"+e.getItemName()+"</td>");
					
//					out.println("<td><form name='"+e.getDateCreated()+"' method='GET' action='/redirect'>");
//					out.println("<input type='hidden' name='page' value='rating'>");
//					out.println("<input type='hidden' name='uid' value='"+a.getUserId()+"'>");
//				    out.println("<A HREF='javascript:document."+e.getDateCreated()+".submit()'>"+a.getUserId()+"</A>");
//				    out.println("</form></td>");
					out.println("<td><input type='button' id= '"+e.getDateCreated()+"' onclick= 'viewRating(this)' value='"+a.getUserId()+"'/input></td>");
//					out.println("<td><input type='button' id='"+e.getDateCreated()+"' onclick='viewRating(this)' value='"+a.getUserId()+"'></input></td>");
					out.println("<td>"+e.getPrice()+"</td>");
					out.println("<td>"+e.getDateCreated()+"</td>");
					out.println("<td>"+a.getStatus()+"</td>");
					out.println("<td>"+b.getPrice()+"</td>");					
					out.println("<td><input type='button' id= '"+e.getAuctionId()+"' data-bp='"+i.getBasePrice()+"' data-hp='"+b.getPrice()+"' onclick= 'bidItem(this)' value='Bid Now'/input></td>");
					out.println("</tr>");
				} catch (EntityNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			out.println("</table>");
		
		}
	}

}
